﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Nissen_Manage.Forms;
using Nissen_Model;
using PosHacker.Form;

namespace Nissen_Marking.Forms
{
    public partial class FormAnswerPaper : Form
    {
        public string SubjectCourseId { get; set; }
        //public IList<SubjectCourse> SubjectCourseList { get; set; }
        public IList<FormSetCourse> FormSetCourses { get; set; }
        public string ParaDate { get; set; }

        public string PageNum { get; set; }

        private AnswerPaperService _answerPaperService;

        private BindingSource _bindingSource;

        public FormAnswerPaper()
        {
            InitializeComponent();
            _answerPaperService = new AnswerPaperService();
            _bindingSource = new BindingSource(); 
        }

        private IList<AnswerPaper> Sort(IList<AnswerPaper> list)
        {
            
            if(list == null || list.Count==0)
                return new List<AnswerPaper>();


            IList<AnswerPaper> res = new List<AnswerPaper>();
            
            foreach (var answerPaper in list)
            {
                var cnt = res.Count;
                for (int i = 0; i < cnt; i++)
                {
                   

                    //if (string.CompareOrdinal(answerPaper.FileName, res[i].FileName) > 0){
                    if (answerPaper.FileName.Equals(res[i].FileName)) {
                        break;

                    }else if (answerPaper.FileName.CompareTo( res[i].FileName) > 0)
                    {
                        if (i == cnt - 1)
                            res.Add(answerPaper);
                        else
                            continue;
                    }

                    else if (answerPaper.FileName.CompareTo(res[i].FileName) < 0)
                    { 
                        res.Insert(i, answerPaper); 
                        break; 
                    }
                        


                }
                if (res.Count == 0)
                    res.Add(answerPaper);



            }

            return res;
        }

        private void FormAnswerPaper_Load(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            if(String.IsNullOrEmpty(SubjectCourseId))
            {


                IList<AnswerPaper> itemlist = new List<AnswerPaper>();



                IList<string>   tmplist = new List<string>();

                foreach (var item in FormSetCourses)
                {
                    if (tmplist.Contains(item.subjectCourseTestId))
                        continue;
                    else
                        tmplist.Add(item.subjectCourseTestId);

                    foreach (var answerPaper in this._answerPaperService.GetAnswerPapersBySubjectCourseId(item.subjectCourseTestId))
                    {
                        if (answerPaper.AnswerPaperPath.Contains(ParaDate))
                            itemlist.Add(answerPaper); 
                    }
                }
                var tmpList =
                    this.Sort(itemlist);
                _bindingSource.DataSource = tmpList;
                this.dataGridView1.DataSource = _bindingSource;
            }
            else
            {
                var list =
                    this._answerPaperService.GetAnswerPapers(this.SubjectCourseId,this.PageNum);

                _bindingSource.DataSource = list;
                this.dataGridView1.DataSource = _bindingSource;
            }


        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                var item =
                    this.dataGridView1.Rows[e.RowIndex].DataBoundItem as AnswerPaper;
                //if(item != null)
                //    this.axPreview1.ShowFile(item.AnswerPaperPath, 0);
                if (item != null)
                {

                    pictureBox1.LoadAsync(item.AnswerPaperPath);

                }

            }

        }
    }


}
